﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginInterFace.BLL;

namespace PickExperts2.form
{
    public partial class fmPicker_CC : Form
    {
        public fmPicker_CC()
        {
            InitializeComponent();
        }

        private void fmPicker_CC_Load(object sender, EventArgs e)
        {
            Class.IniFile cIniFile = new Class.IniFile(Class.main.iniFPath);
            if (cIniFile.ReadInivalue("PickExperts2", "Picker_Check").ToLower() == "true")
                checkBox1.Checked = true;
            textBox1.Text = cIniFile.ReadInivalue("PickExperts2", "Picker_Red").ToLower();
            textBox2.Text = cIniFile.ReadInivalue("PickExperts2", "Picker_Blue").ToLower();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i, j, n,m;
            long x;
            PluginInterFace.BLL.picker bp = new PluginInterFace.BLL.picker();
            PluginInterFace.BLL.ssq bs = new PluginInterFace.BLL.ssq();
            List<int> list = new List<int>();
            string tmp;
            string[] strtmp ;
            textBox8.Text = "";
            progressBar1.Minimum = 0;
            progressBar1.Maximum = (int)numericUpDown6.Value - 1;
            label56.Text = "号码生成中....";
            string[] a = textBox1.Text.Split(',');
            if (a.Length < 6)
            {
                MessageBox.Show("红球数据错误", "消息");
                return;
            }
            a = textBox2.Text.Split(',');
            if (a.Length < 1)
            {
                MessageBox.Show("蓝球数据错误", "消息");
                return;
            }
            loadingCircle1.Active = true;
            for (i = 0, x = 0; i < numericUpDown6.Value && x <= 17721088; i++, x++)
            {
                list = bp.GetRandomArray(textBox1.Text, 6);
                bp.Sort(list);
                list.Add(bp.GetRandomArray(textBox2.Text, 1)[0]);
                if (checkBox1.Checked && bs.ExistsData(list[0], list[1], list[2], list[3], list[4], list[5], list[6]))
                {//历史上出现过
                    i--;
                    continue;
                }
                tmp = "";
                for (j = 0; j < list.Count; j++)
                {
                    if (j == 5)
                        tmp += list[j].ToString().PadLeft(2, '0') + ":";
                    else
                        tmp += list[j].ToString().PadLeft(2, '0') + " ";
                }
                if (textBox8.Text.IndexOf(tmp) >= 0)
                {//列表中出现过
                    i--;
                    continue;
                }
                //缠禅公式规则
                if (textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "")
                {
                    //公式零
                    n = 0;
                    strtmp = textBox3.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown1.Value)
                    {
                        i--;
                        continue;
                    }
                    //公式一
                    n = 0;
                    strtmp = textBox4.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown2.Value)
                    {
                        i--;
                        continue;
                    }
                    //公式二
                    n = 0;
                    strtmp = textBox5.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown3.Value)
                    {
                        i--;
                        continue;
                    }
                    //公式三
                    n = 0;
                    strtmp = textBox6.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown4.Value)
                    {
                        i--;
                        continue;
                    }
                    //公式四
                    n = 0;
                    strtmp = textBox7.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown5.Value)
                    {
                        i--;
                        continue;
                    }
                }
                //缠禅升级公式规则
                if(textBox11.Text!="" && textBox12.Text!="" && textBox13.Text!="")
                {
                    //升级公式零
                    n = 0;
                    strtmp = textBox11.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown7.Value)
                    {
                        i--;
                        continue;
                    }
                    //升级公式一
                    n = 0;
                    strtmp = textBox12.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown8.Value)
                    {
                        i--;
                        continue;
                    }
                    //升级公式二
                    n = 0;
                    strtmp = textBox13.Text.Split(' ');
                    for (m = 0; m < strtmp.Length; m++)
                    {
                        if (tmp.IndexOf(strtmp[m]) >= 0)
                            n++;
                    }
                    if (n > numericUpDown8.Value)
                    {
                        i--;
                        continue;
                    }
                }
                //和值限制
                n = 0;
                if (textBox9.Text != "" && textBox10.Text != "")
                {
                    //strtmp = tmp.Trim().Replace(':', ' ').Split(' ');
                    strtmp = tmp.Trim().Substring(0, tmp.Trim().LastIndexOf(':')).Split(' ');
                    //MessageBox.Show(strtmp[0] + strtmp[1]+strtmp[2]+strtmp[3]+strtmp[4]+strtmp[5]);
                    n = 0;
                    for (int z = 0; z < strtmp.Length; z++)
                    {
                        n += int.Parse(strtmp[z]);
                    }
                    if (n < int.Parse(textBox9.Text) || n > int.Parse(textBox10.Text))
                    {
                        i--;
                        continue;
                    }
                }
                //奇偶限制
                if(!radioButton1.Checked)
                {
                    if(n==0)
                    {
                        strtmp = tmp.Trim().Substring(0, tmp.Trim().LastIndexOf(':')).Split(' ');
                        //MessageBox.Show(strtmp[0] + strtmp[1]+strtmp[2]+strtmp[3]+strtmp[4]+strtmp[5]);
                        for (int z = 0; z < strtmp.Length; z++)
                        {
                            n += int.Parse(strtmp[z]);
                        }
                    }
                    if(radioButton2.Checked)
                    {
                        //奇
                        if (n % 2 == 0)
                        {
                            i--;
                            continue;
                        }
                    }
                    if(radioButton3.Checked)
                    {
                        //偶
                        if (n % 2 != 0)
                        {
                            i--;
                            continue;
                        }
                    }
                }
                //通过规则
                Application.DoEvents();
                progressBar1.Value = i;
                label56.Text = string.Format("号码生成中({0}/{1})....", i + 1, numericUpDown6.Value);
                textBox8.Text += tmp + "\r\n";
            }
            label56.Text = "号码生完毕";
            loadingCircle1.Active = false;
            MessageBox.Show("号码生成完毕", "消息");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox8.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ssq_pick ssp = new ssq_pick();
            PluginInterFace.Model.ssq_pick[] ssm;
            string[] tmp;
            string 期号 = "";
            if (textBox8.Text != "")
            {
                期号 = control.InputBox.ShowInputBox("输入期号:", "期号:", DateTime.Now.Year.ToString());
                if (期号 != "")
                {
                    tmp = textBox8.Lines;
                    ssm = new PluginInterFace.Model.ssq_pick[tmp.Length];
                    for (int i = 0; i < tmp.Length; i++)
                    {
                        ssm[i] = new PluginInterFace.Model.ssq_pick();
                        ssm[i].期号 = int.Parse(期号);
                        ssm[i].号码 = tmp[i];
                    }
                    if (ssp.AddData(ssm))
                        MessageBox.Show("添加成功", "消息");
                    else
                        MessageBox.Show("添加失败", "消息");
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            textBox1.Text =
                "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33";
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            textBox2.Text = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16";
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            textBox9.Text = "";
            textBox10.Text = "";
        }
    }
}
